package com.conero.yangsu.controller.system;

import com.conero.yangsu.entity.AdminEntity;
import com.conero.yangsu.lib.AdminSecurity;
import com.conero.yangsu.lib.FeedbackData;
import com.conero.yangsu.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@RestController
@RequestMapping("/system")
public class LoginController {
    @Autowired
    private AdminMapper adminMapper;
    @Value("${yscms.dev.auto-login:false}")
    private boolean autoLogin;  // 自动登录


    @GetMapping("/login")
    public FeedbackData login(@RequestParam(value = "account", defaultValue = "") String account,
                              @RequestParam(value = "password", defaultValue = "") String password,
                              HttpSession session) {
        if (account.isEmpty() || password.isEmpty()) {
            return FeedbackData.error("请求参数缺失，请参文档！");
        }

        AdminEntity admin = adminMapper.findByAccount(account);
        if (admin == null) {
            return FeedbackData.error("用户或密码错误！");
        } else if (!AdminSecurity.verifyPassword(password, admin)) {
            return FeedbackData.error("用户或密码错误！");
        }

        if (admin.getIsValid() != 1) {
            return FeedbackData.error("账号已被禁用！");
        }

        session.setAttribute(AdminSecurity.AdminSessionKey, AdminSecurity.makeAdminSessionData(admin));
        return FeedbackData.success("用户登录成功！");
    }

    @GetMapping("/login/quit")
    public FeedbackData quit(@RequestParam(value = "goto", defaultValue = "") String gotoUrl,
                             HttpServletResponse response,
                             HttpSession session) throws IOException {
        if (null != session.getAttribute(AdminSecurity.AdminSessionKey)) {
            // 移除session
            session.removeAttribute(AdminSecurity.AdminSessionKey);
            if(!gotoUrl.isEmpty()){
                response.sendRedirect(gotoUrl);
            }
            return FeedbackData.success("用户退出成功！");
        }
        if(!gotoUrl.isEmpty()){
            response.sendRedirect(gotoUrl);
        }
        return FeedbackData.success("您还未登录系统！");
    }

    @RequestMapping(value = "/login/overdue", method = {RequestMethod.GET, RequestMethod.POST})
    public FeedbackData overdue() {
        return FeedbackData.error("您的登录状态已过期请重新登录！");
    }

    @GetMapping("/userInfo")
    public FeedbackData info(HttpSession session) throws SQLException, ClassNotFoundException {
        Object admin = AdminSecurity.getAdminInfo(session);
        if(admin == null && autoLogin){ // 用户测试环境下
            AdminSecurity.autoLoginByUid(1L, adminMapper, session);
            admin = AdminSecurity.getAdminInfo(session);
        }
        if (null == admin) {
            return FeedbackData.error("您的登录状态已失效请重新登录！");
        }
        return FeedbackData.success(admin, "信息获取成功");
    }
}
